const reducers = {
  todos(state, action) {
    const {
      type,
      payload
    } = action

    switch (type) {
      case "set":
        return payload
      case "add":
        return [...state, payload]
      case "remove":
        return state.filter((todo) => todo.id !== payload)
      case "toggle":
        return state.map((todo) => {
          return todo.id === payload ?
            Object.assign({}, todo, {
              complete: !todo.complete,
            }) :
            todo
        })
      default:
        return state
    }
  },
  incrementCount(state, action) {
    const {
      type
    } = action
    switch (type) {
      case "set":
        return state + 1
      case "add":
        return state + 1

      default:
        return state
    }
  },
}
const combineReducers = (reducers) => {
  return (state, action) => {
    const newState = {}
    const keys = Object.keys(reducers)
    keys.forEach((key) => {
      newState[key] = reducers[key](state[key], action)
    })
    return {
      ...state,
      ...newState,
    }
  }
}
export default combineReducers(reducers)